using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 魔法免疫Buff - 免疫所有魔法效���
/// </summary>
public class MagicImmuneBuff : BuffBase
{
    private bool m_OriginalMagicImmune;

    public MagicImmuneBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 设置魔法免疫状���
        Target.HealthComponent.SetImmuneToMagic(true);
        
        // 播放魔法免疫特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("magicImmune", true);
        }
        
        Logger.Log($"{Target.name} 获得魔法免疫，免疫所有魔法效���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 魔法免疫期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 魔法免疫效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 移除魔法免疫状���
        Target.HealthComponent.SetImmuneToMagic(false);
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("magicImmune", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("MagicImmune"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 魔法免疫效果不能叠加，只能刷新持续时���
    }
}
